﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using ProjectCommon;

namespace ProjectShop
{
    public partial class FrmTaskShop : Form
    {
        ProjectCommon.ClassCommon cc;

        public FrmTaskShop(ProjectCommon.ClassCommon aClassCommon)
        {
            InitializeComponent();
            cc = aClassCommon;
            this.DialogResult = DialogResult.Cancel;
        }

        private void rbShop_CheckedChanged(object sender, EventArgs e)
        {
            string sql = @"select tbshopno,tbshopname,tbfastcode from ShopInfo";
            SqlDataAdapter sdaShop = new SqlDataAdapter(sql, cc.scKengee_IT_MIS);
            DataTable dtShop=new DataTable();
            cc.scKengee_IT_MIS.Open();
            sdaShop.Fill(dtShop);
            cc.scKengee_IT_MIS.Close();
            sdaShop.Dispose();

            treeView1.Nodes.Clear();
            for (int i = 0; i < dtShop.Rows.Count; i++)
            {
                string sShopName = dtShop.Rows[i]["tbshopno"].ToString() + dtShop.Rows[i]["tbshopname"].ToString();
                string sFastCode=dtShop.Rows[i]["tbFastCode"].ToString();
                TreeNode tnNew = treeView1.Nodes.Add(sShopName);
                tnNew.Tag = sFastCode;
            }
        }

        private void tbFind_TextChanged(object sender, EventArgs e)
        {
            if (tbFind.Text.Trim().Length > 0)
            {
                for (int i = 0; i < treeView1.Nodes.Count; i++)
                {
                    string CurrentShopName = treeView1.Nodes[i].Text;
                    if (CurrentShopName.Contains(tbFind.Text.Trim()))
                    {
                        treeView1.SelectedNode = treeView1.Nodes[i];                
                        break;
                    }
                }
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Text = treeView1.SelectedNode.Tag.ToString();
            this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void treeView1_DoubleClick(object sender, EventArgs e)
        {
            btnOK.PerformClick();
        }

        private void tbFind_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar == 13)&&(treeView1.SelectedNode!=null))
            {
                btnOK.PerformClick();
            }
        }        
    }
}
